package com.ybfenpan.controller.mall;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Member;
import com.ybfenpan.model.mall.MyAddress;
import com.ybfenpan.model.shop.Goods;
import com.ybfenpan.model.shop.Orders;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.mall.MyAddressService;
import com.ybfenpan.service.shop.GoodsService;
import com.ybfenpan.service.shop.OrdersService;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
public class MallGoodsController extends BaseController {
	@Resource
	private GoodsService goodsService;
	@Resource
	private MemberService memberService;
	@Resource
	private OrdersService ordersService;

	@Resource
	private MyAddressService myAddressService;

	@RequestMapping("mall/placeOrderShow.shtml")
	public String pay(HttpServletRequest request, ModelMap model)
			throws Exception {
		Integer type = 0;

		Boolean result = true;
		// 代理商信息
		Member member = ContextUtil.getMember();
		member = memberService.getByKey(member.getId());

		Map formMap = getParameterMap(request);

		Integer goodsId = MapUtil.getIntValue(formMap, "goodsId");
		Integer myAddressId = MapUtil.getIntValue(formMap, "myAddressId");

		if (0 < goodsId) {
			Goods goods = goodsService.getByKey(goodsId);
			if (goods.getIsActive().equals("T")) {
				request.setAttribute("goods", goods);
				formMap.put("goodsId", goodsId);
				// 转账金额验证
				Integer num = 1;
				if (num < 0) {
					setMessage(model, "请输入正确的购买数量。");
					result = false;
				} else {
					if (num > goods.getTotalNum()) {
						setMessage(model, "库存量不足。");
						result = false;
					} else {
						// 转账类型
						int payWay = 10;
						String personName = "";
						String address = "";
						String tel = "";
						String code = "";
						MyAddress myAddress = this.myAddressService
								.getByKey(myAddressId);
						if (myAddress == null) {

							setMessage(model, "请填写收货地址");
							result = false;
						} else {

							personName = myAddress.getPersonName();
							address = myAddress.getProvince()
									+ myAddress.getAddress();
							tel = myAddress.getTel();
							code = myAddress.getCode();

							if (payWay == 10) {
								type = 10;
								// 转账
								if (member.getEle() < num * goods.getPrice()) {
									setMessage(model, "电子积分余额不足");
									result = false;
								} else {
									Random random = new Random();
									SimpleDateFormat formatter = new SimpleDateFormat(
											"yyyyMMddHHmmss");
									String orderNo = "B"
											+ formatter.format(new Date())
											+ random.nextInt(2);

									Orders orders = new Orders();
									orders.setOrderNo(orderNo);
									orders.setGoodsPrice(goods.getPrice());
									orders.setGoodsTitle(goods.getTitle());
									orders.setBuyNum(num);
									orders.setPayPrice(num * goods.getPrice());
									orders.setPayWay(payWay);
									orders.setPersonName(personName);
									orders.setAddress(address);
									orders.setTel(tel);
									orders.setCode(code);
									// orders.setNote(note);
									orders.setMemberId(member.getId());
									this.ordersService.saveweiyi(member,orders, payWay,num * goods.getPrice(), orderNo,goodsId, type, num);
									return "redirect:home.shtml";
								}
							}
						}

					}
				}

			}
		}

		if (!result) {
			formMap = getParameterMap(request);
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("memberId", member.getId());
			List<MyAddress> members = myAddressService.queryForList("select", param);
			
			if (members!=null&&members.size()>0) {
				MyAddress myAddress=members.get(0);
				if (myAddress!=null) {
					request.setAttribute("myAddress",myAddress);
				}
			}
			return "mall/placeOrder";
		}

		return "mall/home";
	}

}
